<%-- 
 *Workshop 
 *Class ID        : SE0691   
 *Student ID      : SE90009
 *Student Name    : Phạm Nguyễn Nhật Trung
 *Due Date        : Nov 21, 2012, 9:15:45 PM
 *I declare that this assignment is my own work
 *in accordance with FPT Policy.
--%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="tracker.stub.UsersSessionStub"%>
<%@page import="tracker.entity.UsersEntity"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <LINK REL=StyleSheet HREF="style/style.css" TYPE="text/css" MEDIA=screen>
        <title>Tracker - Registration</title>
        <style>
            body{
                background: none;
            }
        </style>
    </head>
    <body>
        <div class="center-form">
            <h1>Account Registration Page</h1>
            <div id="form-register" style="">
                <Form action ='' method='post'>
                    <table border='0' style="padding-top: 5px;">
                        <tr>
                            <td>Username: </td>
                            <td><input type='text' name ='username' style="width: 100%" value=""></td>
                        </tr>
                        <tr>
                            <td>Password: </td>
                            <td><input type ='password' name='password' style="width: 100%" value=""></td>
                        </tr>
                    </table>
                    <br><input class = "bg-color-green" style="color: #fff;" type ='submit' value='Register'>
                    <input class = "bg-color-purple" style="color: #fff;" type='reset'value='Reset'>
                    <button class = "bg-color-gray" type="button" name="Login" style="color: #fff;" onclick="parent.location='login.jsp'">Or, Login Existed Account</button>
                </form>
            </div>
        </div>
        <%
            String name = "", password = "";
            response.setContentType("text/html;charset=UTF-8");
            try {
                /* TODO output your page here. You may use following sample code. */
                if (request.getParameter("username") != null) {
                    try {
                        name = request.getParameter("username").trim(); //Get input
                        password = request.getParameter("password").trim(); //Get input
                    } catch (Exception e) {
                        out.println("<div style=\"width:800px; margin:0 auto; padding-top: 250px\"><h3>" + e.getMessage() + "</h3></div>");
                    }
                }
            } catch (Exception e) {
                out.println("<div style=\"width:800px; margin:0 auto; padding-top: 250px\"><h3>" + e.getMessage() + "</h3></div>");
            } finally {
                //out.close();
            }
            Pattern p = Pattern.compile("^[A-Za-z0-9]+$"); //Check invalid character
            Matcher m = p.matcher(name);
            if (!name.isEmpty()) {
                if (m.matches()) {
                    UsersSessionStub stub = new UsersSessionStub();
                    if (!stub.checkIsExistedUsername(name)) { //Check whether username is existed or not
                        try {
                            UsersEntity newUser = new UsersEntity();
                            newUser.setUserName(name);
                            newUser.setPassword(password);
                            newUser.setUserLevel(3);
                            newUser.setSeedAmount(1000.0F);
                            newUser.setLeechAmount(100.0F);
                            newUser.setCommentsEntityCollection(null);
                            newUser.setPointsEntityCollection(null);
                            newUser.setUserId(0);
                            stub.insert(newUser); //Insert to Database
                            out.println("<div class=\"center-form\" <h2>Registered Successfully!</h2> </div>");
                        } catch (Exception e) {
                        }
                    } else {
                        out.println("<div class=\"center-form\" <h2>Username is existed!</h2> </div>");
                    }
                } else {
                    out.println("<div class=\"center-form\" <h2>Invalid Characters!</h2> </div>");
                }

            }

        %>

    </body>
</html>
